class AdminController < ApplicationController
  before_filter :authorize
  
  def index
    list 
    render :action => 'list'
  end

  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
    find_condition
    @articles = Article.find_articles_by_receiveddate ( params[:startdate] , params[:enddate])
    p @articles
    @departments = Department.find_all
  end
  
  def find_condition
    
    @fromdate = params[:startdate]
    @todate = params[:enddate]
    if(@fromdate==nil or @fromdate.empty? )
      @fromdate=Time.now.at_beginning_of_year.to_date
    end
    if(@todate==nil or @todate.empty? )
      @todate=Time.now.to_date
    end

    params[:startdate] = @fromdate
    params[:enddate] = @todate
    
  end

  def show
    @article = Article.find(params[:id])
    @departments = Department.find_all
  end

  def new
    @article = Article.new
    @departments = Department.find_all

  end

  def create
    @article = Article.new(params[:article])
    begin
      if @article.save
        flash[:notice] = 'Successful!'
        redirect_to :action => 'list'
      else
        render :action => 'new'
      end
    rescue
        flash[:notice] = 'Failed! Please check the fields of your articles.'
        redirect_to :action => 'list'
    end

  end

  def edit
    @article = Article.find(params[:id])
    @departments = Department.find_all

  end

  def update
    @article = Article.find(params[:id])
    if @article.update_attributes(params[:article])
      flash[:notice] = 'Article was successfully updated.'
      redirect_to :action => 'index'
    else
      render :action => 'edit'
    end
  end
  def search
    find_condition
    @articles = Article.find_articles_by_receiveddate ( params[:startdate] , params[:enddate])
    @departments = Department.find_all
    render :partial =>"search", :object => @articles,:departments =>@departments, :layout => false
  end
  def destroy
    Article.find(params[:id]).destroy
    redirect_to :action => 'list'
  end
  
  def get_revision_info
    puts 
    @revisions = RevisionRecord.find_all_by_article_id(params[:article_id])
    puts @revisions.length
    render :partial =>"revision_list", :object => @revisions,:layout => false
  end
end
